package org.example.lease.service.dutyhandler.loginduty.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.example.common.exception.LeaseException;
import org.example.common.util.JWTUtil;
import org.example.lease.model.entities.SystemUser;
import org.example.lease.model.result.ResultCodeEnum;
import org.example.lease.service.dutyhandler.loginduty.AbstractLoginHandler;
import org.example.lease.vo.login.LoginVo;
import org.springframework.stereotype.Component;

/**
 * 密码验证处理器
 */
@Component
public class PasswordValidateHandler extends AbstractLoginHandler {

    @Override
    protected String doHandle(LoginVo loginVo) {
        SystemUser systemUser = loginVo.getSystemUser();
        String passwordFromVo = loginVo.getPassword();
        String encodePassword = DigestUtils.md5Hex(passwordFromVo);
        if (!encodePassword.equals(systemUser.getPassword())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        // 验证通过，生成token
        return JWTUtil.createToken(systemUser.getId(), systemUser.getUsername());
    }
}